<template>
  <el-container>
    <basic-crud :option="option" ref="basicCrudRef" businessUrl="/permission/passwordRuleController">
      <template #menu="{ row, size, type }">
        <el-button :size="size" link :type="type" v-if="!row.defaultFlag" @click="handleSetDefault(row)"
          :loading="row.loading">默认</el-button>
      </template>
    </basic-crud>
  </el-container>
</template>

<script setup>
import {
  apiSetDefault,
} from '@/apis/system/passwordRule.js'

const option = {
  searchShow:false,
  statusBtn: true,
  menuWidth: 300,
  calcHeight:10,
  column: [
    {
      label: '编号',
      prop: 'code',
      search: true,
      width: 120,
      rules: [
        {
          required: true,
          message: '编号不能为空',
          trigger: 'blur',
        },
      ],
    },
    {
      label: '名称',
      prop: 'name',
      search: true,
      width: 150,
      rules: [
        {
          required: true,
          message: '名称不能为空',
          trigger: 'blur',
        },
      ],
    },
    {
      label: '最小长度',
      prop: 'minLength',
      type: 'number',
      value: 6,
      min: 1,
      width: 100,
    },
    {
      label: '最大长度',
      prop: 'maxLength',
      type: 'number',
      value: 32,
      min: 1,
      width: 100,
    },
    {
      label: '有效天数',
      prop: 'validDay',
      type: 'number',
      width: 100,
    },
    {
      label: '提醒天数',
      prop: 'remindDay',
      placeHolder: '到期日期提前多少天开始提醒',
      type: 'number',
      width: 100,
    },
    {
      label: '密码可重试次数',
      prop: 'retryTime',
      append: '次',
      type: 'number',
      min: 1,
      value: 5,
      width: 150,
    },
    {
      label: '锁定时间',
      prop: 'lockTime',
      append: '分',
      type: 'number',
      min: 1,
      value: 10,
      width: 150,
    },
    {
      label: '状态',
      prop: 'status',
      slot: 'status',
      display: false,
      width: 100,
    },
    {
      label: '是否默认',
      prop: 'defaultFlag',
      type: 'switch',
      width: 100,
      readOnly: true,
      display: false,
      value: 'false',
      virtualize: true,
      dicData: [
        {
          label: '是',
          value: 'true',
        },
        {
          label: '否',
          value: 'false',
        },
      ],
      formatter: (val) => {
        if (val.defaultFlag == 'true' || val.defaultFlag === true) {
          return '是'
        } else {
          return '否'
        }
      },
    },
    {
      label: '密码所需字符',
      prop: 'requireCharType',
      type: 'select',
      width: 150,
      multiple: true,
      virtualize: true,
      dicData: [
        {
          label: '大写字母',
          value: 'upLetter',
        },
        {
          label: '小写字母',
          value: 'lowLetter',
        },
        {
          label: '数字',
          value: 'number',
        },
        {
          label: '特殊字符',
          value: 'character',
        },
      ],
    },
    {
      label: '密码所需字符种类',
      prop: 'requireCharCount',
      append: '种',
      type: 'number',
      min: 1,
      max: 4,
      value: 1,
      width: 150,
    },
    {
      label: '密码相同次数',
      prop: 'sameLastCount',
      append: '次',
      type: 'number',
      min: -1,
      value: -1,
      width: 150,
    },
    {
      label: '描述',
      prop: 'des',
      minWidth: 150,
      span: 24,
      type: 'textarea',
      minRows: 3,
      maxRows: 5,
    },
  ],
}

const basicCrudRef = ref()

// 设置为默认值
const handleSetDefault = (row) => {
  row.loading = true
  apiSetDefault(row)
    .then(() => {
      ElMessage.success('操作成功！')
      basicCrudRef.value && basicCrudRef.value.getList()
    })
    .finally(() => {
      row.loading = false
    })
}
</script>

<style lang="scss" scoped>
.el-container {
  padding: 10px;
  box-sizing: border-box;
  background-color: #fff;
}
</style>
